#$dir = '/data1/raw/ngs/illumina-ga-1/bioinf_int/MayoGAP/mayogap_emerge/clean/beagle/run_beagle/beagle_mainrun';
$dir = '/data1/raw/ngs/illumina-ga-1/bioinf_int/MayoGAP/mayogap_vte/clean/beagle';
#$i=0;
for($i1=0;$i1<22;$i1++)
{
	$j = $i1+1;
	$tdir = "$dir/$j";
	$tm ="ls -p $tdir| grep \"/\"|/usr/bin/perl -e '".'while($l=<STDIN>) {chomp $l;$l =~ s/\///g; if($l =~ m/\d/) {print $l."\n"; }}'."' > $tdir/temp_num";
	system($tm);
	open BUFF,"<$tdir/temp_num" or die "Can't open $tdir/temp_num : $!";
	@{"array".$j} = <BUFF>;
	system("rm $tdir/temp_num");
	$line = join("",@{"array".$j});
	@{"array".$j} = split("\n",$line);
	@{"array".$j} = sort {$a <=> $b} @{"array".$j};
	
	for($i=0;$i<@{"array".$j};$i++)
	{
		$temp1 = ${"array".$j}[$i];
		system("mv $tdir/$temp1/runbeagle$temp1.e* $tdir/$temp1/runbeagle$temp1.e");
		open(BUFF,"$tdir/$temp1/runbeagle$temp1.e");
		#system("rm -rf $tdir/$temp1/runbeagle2.*");
		$line = <BUFF>;
		if($line =~ m/Allele in data file but not in marker file:/)
		{

			@array = split(/\s/,$line);
			#print @array[@array-1]."\n";
			system("gunzip /bin/gzip $tdir/$temp1/*.gz");
			open(WRBUFF,">/data1/raw/ngs/illumina-ga-1/bioinf_int/MayoGAP/mayogap_emerge/clean/beagle/run_beagle/beagle_mainrun/snplist");
			print WRBUFF @array[@array-1]."\n";
			system("mv $tdir/$temp1/geno_segment$temp1.bgl $tdir/$temp1/geno_segment$temp1.1.bgl");
			system("/usr/bin/perl perl_flip_snp.pl -s data1/raw/ngs/illumina-ga-1/bioinf_int/MayoGAP/mayogap_emerge/clean/beagle/run_beagle/beagle_mainrun/snplist -f $tdir/$temp1/geno_segment$temp1.1.bgl -n $tdir/$temp1/geno_segment$temp1.bgl ");
			system("/bin/gzip $tdir/$temp1/*.bgl");
			
			system("/home/sge6_2/bin/lx24-amd64/qsub runbeagle$temp1");

	#	print "success\n";
		}
		
	}
	 
}
